package com.msb.mall.gateway.filter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * @author WGG
 * @create 2023-03-25 21:35
 */

/**
 * 跨域的配置类
 */
@Configuration
public class MallCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        // CorsConfigurationSource接口的实现类 设置跨域条件
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //设置配置条件
        CorsConfiguration configuration = new CorsConfiguration();
        //设置允许访问的请求头信息
        configuration.addAllowedHeader("*");
        //设置允许访问的请求方式
        configuration.addAllowedMethod("*");
        //设置允许访问的请求来源
        configuration.addAllowedOrigin("*");
        //设置允许访问的请求携带cookie跨域
        configuration.setAllowCredentials(true);
        // 设置注册跨域的路径和配置
        source.registerCorsConfiguration("/**",configuration);
        //返回跨域的条件
        return new CorsWebFilter(source);
    }
}
